package com.example.conusmer.controller

import com.example.conusmer.param.CustomerParams
import com.example.conusmer.entity.Customer
import com.example.conusmer.service.CustomerService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*

@RestController
@RequestMapping("/api/customers")
class CustomerController() {
    @Autowired
    lateinit var customerService: CustomerService

    @PostMapping("/list")
    fun getAllCustomers(@RequestBody queryParams: CustomerParams): ResponseEntity<List<Customer>> {
       return ResponseEntity.ok(customerService.getAllCustomers(queryParams))
    }
    @PostMapping
    fun createCustomer(@RequestBody param: CustomerParams): ResponseEntity<Void> {
        customerService.createCustomer(
            Customer(
                name =    param.name,
                email =    param.email,
            ))
        return  ResponseEntity.noContent().build()
    }



    @PutMapping("/{id}")
    fun updateCustomer(@PathVariable id: Long, @RequestBody param: CustomerParams): ResponseEntity<Void> {
        customerService.updateCustomer( Customer(
            id=  id,
            name =    param.name,
            email =    param.email,
        ))

        return ResponseEntity.noContent().build()
    }

    @DeleteMapping("/{id}")
    fun deleteCustomer(@PathVariable id: Long): ResponseEntity<Void> {
        customerService.deleteCustomer(id)
        return ResponseEntity.noContent().build()
    }

    @GetMapping("/get/{id}")
    fun getCustomerById(@PathVariable id: Long): ResponseEntity<Customer> {
        val customer = customerService.findCustomerById(id)
        return ResponseEntity.ok(customer)
    }

}